package com.czy.owner.ui.physicalorder;

import android.content.Intent;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.adapter.PickGoodsCouponAdapter;
import com.czy.owner.entity.GoodsCouponEntity;
import com.czy.owner.entity.GoodsEntity;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangyang on 2017/06/30
 */
public class PickGoodsCouponActivity extends BaseActivity {
    @BindView(R.id.elv_coupon)
    ExpandableListView elvCoupon;
    private List<GoodsCouponEntity> data = null;
    private List<GoodsCouponEntity> mData = new ArrayList<>();
    private GoodsCouponEntity pickedGoodsCoupon;
    private double sumGoodsPrice;
    private PickGoodsCouponAdapter mAdapter;
    private String installGoodIds;
    private List<GoodsEntity> mList;
    private boolean isTransfer;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_pick_coupon;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        if (getIntent().getExtras() == null || (data = (List<GoodsCouponEntity>) getIntent().getSerializableExtra(Constants.TRANSMISSION_CONTENT_KEY)) == null) {
            PhoneUtils.ShowToastMessage(PickGoodsCouponActivity.this, "参数异常!");
            finish();
            return;
        }
        isTransfer = getIntent().getBooleanExtra("isTransfer",false);
        pickedGoodsCoupon = (GoodsCouponEntity) getIntent().getSerializableExtra("goodCoupon");
        mList = (List<GoodsEntity>) getIntent().getSerializableExtra("mList");
        sumGoodsPrice = getIntent().getDoubleExtra("goodPrice", 0);
        installGoodIds = getIntent().getStringExtra("installGoodIds");

        MyLog.e("yang","sumGoodsPrice=="+sumGoodsPrice);
        for (int i = 0; i < data.size(); i++) {
            if(isTransfer){
                double price = getIntent().getDoubleExtra("price",0);
                if (data.get(i).getDenomination() > sumGoodsPrice || data.get(i).getMinimum() > price) {
                    data.remove(i);
                    i--;
                    MyLog.e("yang","i==="+i);
                } else {
                    if (data.get(i).getUseType().equals("only") ) {
                        double mPrice = 0.00;
                        String couponsGoodsId = data.get(i).getGoodsId();
                        if(TextUtils.isEmpty(couponsGoodsId)){

                        }else {
                            String[] split = couponsGoodsId.split(",");
                            boolean isHaveGoodsId = false;
                            for(int m=0;m<split.length;m++){
                                String mGoodsId = split[m];
                                for (int j = 0; j < mList.size(); j++) {
                                    if (mList.get(j).getGoodsId() == Long.valueOf(mGoodsId)) {
                                        mPrice += mList.get(j).getGoodsSpecValuesPrice() * mList.get(j).getCount();
                                    }
                                }
                                if(installGoodIds.contains(mGoodsId)){
                                    isHaveGoodsId = true;
                                }

                            }
                            if(!isHaveGoodsId){
                                data.remove(i);
                                i--;
                            }else {
                                if(data.get(i).getMinimum() > mPrice){
                                    data.remove(i);
                                    i--;
                                }
                            }

                        }
                    }

                }
            }else {
                if (data.get(i).getMinimum() > sumGoodsPrice) {
                    data.remove(i);
                    i--;
                } else {
                    if (data.get(i).getUseType().equals("only") ) {
                        double mPrice = 0.00;
                        String couponsGoodsId = data.get(i).getGoodsId();
                        if(TextUtils.isEmpty(couponsGoodsId)){

                        }else {
                            String[] split = couponsGoodsId.split(",");
                            boolean isHaveGoodsId = false;
                            for(int m=0;m<split.length;m++){
                                String mGoodsId = split[m];
                                for (int j = 0; j < mList.size(); j++) {
                                    if (mList.get(j).getGoodsId() == Long.valueOf(mGoodsId)) {
                                        mPrice += mList.get(j).getGoodsSpecValuesPrice() * mList.get(j).getCount();
                                    }
                                }
                                if(installGoodIds.contains(mGoodsId)){
                                    isHaveGoodsId = true;
                                }

                            }
                            if(!isHaveGoodsId){
                                data.remove(i);
                                i--;
                            }else {
                                if(data.get(i).getMinimum() > mPrice){
                                    data.remove(i);
                                    i--;
                                }
                            }

                        }
                    }

                }
            }


        }

        Collections.sort(data, new Comparator<GoodsCouponEntity>() {
            @Override
            public int compare(GoodsCouponEntity o1, GoodsCouponEntity o2) {
                if (o1.getMinimum() > sumGoodsPrice && o2.getMinimum() <= sumGoodsPrice) {
                    return 1;
                } else if (o1.getMinimum() <= sumGoodsPrice && o2.getMinimum() > sumGoodsPrice) {
                    return -1;
                } else if (o1.getMinimum() < o2.getMinimum()) {
                    return 1;
                } else if (o1.getMinimum() > o2.getMinimum()) {
                    return -1;
                } else if (o1.getDenomination() < o2.getDenomination()) {
                    return 1;
                } else if (o1.getDenomination() > o2.getDenomination()) {
                    return -1;
                }
                return 0;
            }
        });

        initTitle();
        initView();
    }

    @Override
    protected void updateViews() {

    }


    private void initTitle() {
        getToolbarTitle().setText("选择优惠券");
        getSubTitle().setText("确定");
    }

    @OnClick(R.id.toolbar_subtitle)
    public void sure(View view) {
        Intent intent = new Intent();
        intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, mAdapter.getPickedGoodsCoupon());
        setResult(RESULT_OK, intent);
        finish();
    }


    private void initView() {
        if (data.size() != 0) {
//            PickCouponAdapter mAdapter = new PickCouponAdapter(this,data.getCouponsList(),data.isAllCoupons(),data.getTailAmount());
            mAdapter = new PickGoodsCouponAdapter(this, data, pickedGoodsCoupon, sumGoodsPrice);
            elvCoupon.setAdapter(mAdapter);
            for (int i = 0; i < mAdapter.getGroupCount(); i++) {
                elvCoupon.expandGroup(i);
            }
            elvCoupon.setGroupIndicator(null);
            elvCoupon.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                    return true;
                }
            });
        }
    }


}
